<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- saved from url=(0028)http://jdbi.org/archive.html -->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>


<title>abacus-common</title>

<script type="text/javascript" src="./css/ga.js"></script>
<script type="text/javascript">
	if (top.location != self.location)
		top.location.replace(self.location);
</script>

<link href="./css/css" rel="stylesheet" type="text/css"></link>
<link href="./css/css(1)" rel="stylesheet" type="text/css"></link>
<link rel="stylesheet" href="./css/syntax.css" type="text/css"></link>
<link rel="stylesheet" href="./css/screen.css" type="text/css"></link>

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-65247258-1', 'auto');
  ga('send', 'pageview');
</script>

</head>
<body>
	<div id="site">
		<h1>
			<a href="./index.html">abacus-common</a> <span id="navigation"> <a href="./docs.html">Docs</a> | <a href="./download.html">Download</a>
				| <a href="./api-docs/index.html">Javadoc</a> | <a href="./faq.html">FAQ</a> | <a href="./community.html">Community</a>
			</span>
		</h1>
	</div>
	<p><center><i>(It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures --- Alan Perlis)</i></center></p>

	<div id="content">

		<div id="page">

			<h1>DataSet</h1>
			<p>
				<a href="./DataSet_view.html">DataSet</a> supports distinct/merge/sort/groupBy/join/union/unionAll/except/intersect/paginate/filter/count/toJOSN/toXML/toCVS/... 
				With the methods in <a href="./api-docs/com/landawn/abacus/util/N.html">N</a> and lambda in Java 8, DataSet enlarges the ability to data analyze with simple/concise APIs. An instance of DataSet can be obtained by executing a SQL query (with <a href="./api-docs/com/landawn/abacus/util/JdbcUtil.html">JdbcUtil</a>/<a href="./api-docs/com/landawn/abacus/util/SQLExecutor.html">SQLExecutor</a>), or loading from CSV by <a href="./api-docs/com/landawn/abacus/util/CSVUtil.html">CSVUtil</a>, or parsing JSON by JSONParser, or from a map or a list of entity/map/array with the method <i>asDataSet</i> in class <a href="./api-docs/com/landawn/abacus/util/N.html">N</a>.  
			</p>
<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #AA22FF">@Test</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #B00040">void</span> <span style="color: #0000FF">test_DataSet</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
    Timestamp now <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Timestamp<span style="color: #666666">(</span>System<span style="color: #666666">.</span><span style="color: #7D9029">currentTimeMillis</span><span style="color: #666666">());</span>
    Object<span style="color: #666666">[]</span> parameters1 <span style="color: #666666">=</span> asArray<span style="color: #666666">(</span><span style="color: #BA2121">&quot;fn&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;ln&quot;</span><span style="color: #666666">,</span> UUID<span style="color: #666666">.</span><span style="color: #7D9029">randomUUID</span><span style="color: #666666">().</span><span style="color: #7D9029">toString</span><span style="color: #666666">(),</span> now<span style="color: #666666">,</span> now<span style="color: #666666">);</span>
    Object<span style="color: #666666">[]</span> parameters2 <span style="color: #666666">=</span> asArray<span style="color: #666666">(</span><span style="color: #BA2121">&quot;fn&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;ln&quot;</span><span style="color: #666666">,</span> UUID<span style="color: #666666">.</span><span style="color: #7D9029">randomUUID</span><span style="color: #666666">().</span><span style="color: #7D9029">toString</span><span style="color: #666666">(),</span> now<span style="color: #666666">,</span> now<span style="color: #666666">);</span>
    sqlExecutor<span style="color: #666666">.</span><span style="color: #7D9029">batchInsert</span><span style="color: #666666">(</span>INSERT_ACCOUNT<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">new</span> Object<span style="color: #666666">[]</span> <span style="color: #666666">{</span> parameters1<span style="color: #666666">,</span> parameters2 <span style="color: #666666">});</span>

    String sql <span style="color: #666666">=</span> NE<span style="color: #666666">.</span><span style="color: #7D9029">select</span><span style="color: #666666">(</span>Account<span style="color: #666666">.</span><span style="color: #7D9029">class</span><span style="color: #666666">,</span> asSet<span style="color: #666666">(</span>Account<span style="color: #666666">.</span><span style="color: #7D9029">DEVICES</span><span style="color: #666666">)).</span><span style="color: #7D9029">from</span><span style="color: #666666">(</span>Account<span style="color: #666666">.</span><span style="color: #7D9029">_</span><span style="color: #666666">).</span><span style="color: #7D9029">sql</span><span style="color: #666666">();</span>
    DataSet dataSet <span style="color: #666666">=</span> sqlExecutor<span style="color: #666666">.</span><span style="color: #7D9029">query</span><span style="color: #666666">(</span>sql<span style="color: #666666">);</span>
    dataSet<span style="color: #666666">.</span><span style="color: #7D9029">println</span><span style="color: #666666">();</span>

    String json <span style="color: #666666">=</span> dataSet<span style="color: #666666">.</span><span style="color: #7D9029">toJSON</span><span style="color: #666666">();</span>
    println<span style="color: #666666">(</span>json<span style="color: #666666">);</span>

    String xml <span style="color: #666666">=</span> dataSet<span style="color: #666666">.</span><span style="color: #7D9029">toXML</span><span style="color: #666666">();</span>
    println<span style="color: #666666">(</span>xml<span style="color: #666666">);</span>

    String csv <span style="color: #666666">=</span> dataSet<span style="color: #666666">.</span><span style="color: #7D9029">toCSV</span><span style="color: #666666">();</span>
    println<span style="color: #666666">(</span>csv<span style="color: #666666">);</span>

    PaginatedDataSet paginatedResult <span style="color: #666666">=</span> dataSet<span style="color: #666666">.</span><span style="color: #7D9029">paginate</span><span style="color: #666666">(25);</span>
    println<span style="color: #666666">(</span>paginatedResult<span style="color: #666666">.</span><span style="color: #7D9029">firstPage</span><span style="color: #666666">());</span>

    <span style="color: #408080; font-style: italic">// create account list from data set</span>
    List<span style="color: #666666">&lt;</span>Account<span style="color: #666666">&gt;</span> accounts <span style="color: #666666">=</span> dataSet<span style="color: #666666">.</span><span style="color: #7D9029">toList</span><span style="color: #666666">(</span>Account<span style="color: #666666">.</span><span style="color: #7D9029">class</span><span style="color: #666666">);</span>

    <span style="color: #408080; font-style: italic">// convert account list to data set</span>
    DataSet dataSet2 <span style="color: #666666">=</span> asDataSet<span style="color: #666666">(</span>dataSet<span style="color: #666666">.</span><span style="color: #7D9029">columnNameList</span><span style="color: #666666">(),</span> accounts<span style="color: #666666">);</span>    
    dataSet2<span style="color: #666666">.</span><span style="color: #7D9029">println</span><span style="color: #666666">();</span>
    
    dataSet2<span style="color: #666666">.</span><span style="color: #7D9029">sortBy</span><span style="color: #666666">(</span>asList<span style="color: #666666">(</span><span style="color: #BA2121">&quot;firstName&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;lastName&quot;</span><span style="color: #666666">));</span>

    DataSet dataSet3 <span style="color: #666666">=</span> dataSet2<span style="color: #666666">.</span><span style="color: #7D9029">groupBy</span><span style="color: #666666">(</span><span style="color: #BA2121">&quot;firstName&quot;</span><span style="color: #666666">);</span>
    println<span style="color: #666666">(</span>dataSet3<span style="color: #666666">);</span>

    DataSet dataSet4 <span style="color: #666666">=</span> dataSet2<span style="color: #666666">.</span><span style="color: #7D9029">filter</span><span style="color: #666666">(</span><span style="color: #BA2121">&quot;firstName&quot;</span><span style="color: #666666">,</span> <span style="color: #666666">(</span>String a<span style="color: #666666">)</span> <span style="color: #666666">-&gt;</span> a<span style="color: #666666">.</span><span style="color: #7D9029">startsWith</span><span style="color: #666666">(</span><span style="color: #BA2121">&quot;ab&quot;</span><span style="color: #666666">));</span>
    println<span style="color: #666666">(</span>dataSet4<span style="color: #666666">);</span>

    <span style="color: #B00040">int</span> count <span style="color: #666666">=</span> dataSet2<span style="color: #666666">.</span><span style="color: #7D9029">count</span><span style="color: #666666">(</span><span style="color: #BA2121">&quot;firstName&quot;</span><span style="color: #666666">,</span> <span style="color: #666666">(</span>String a<span style="color: #666666">)</span> <span style="color: #666666">-&gt;</span> a<span style="color: #666666">.</span><span style="color: #7D9029">startsWith</span><span style="color: #666666">(</span><span style="color: #BA2121">&quot;ab&quot;</span><span style="color: #666666">));</span>
    println<span style="color: #666666">(</span>count<span style="color: #666666">);</span>

    DataSet dataSet5 <span style="color: #666666">=</span> dataSet2<span style="color: #666666">.</span><span style="color: #7D9029">intersect</span><span style="color: #666666">(</span>dataSet4<span style="color: #666666">);</span>
    println<span style="color: #666666">(</span>dataSet5<span style="color: #666666">);</span>
<span style="color: #666666">}</span>
</pre></div>


		</div>

	</div>
</body>

</html>